﻿using System;
using arcchallenge.datacontracts;
using arcchallenge.servicecontracts;
using ebc.patterns;

namespace arcchallenge.domainlogicboard
{
    public class DomainLogicBoard : IDomainLogicBoard
    {
        private readonly Action<FileTransformationRequest> in_requestDecomposer;
        private readonly Action<string> in_configMultiplier;

        public DomainLogicBoard(IFileReader reader, IFileWriter writer, ILineTransformator trans)
        {
            var requestDecomposer = new MessageDecomposer<FileTransformationRequest, string, string>(
                (req, out0, out1) =>
                    {
                        out0(req.OutputFilename);
                        out1(req.InputFilename);
                    });
            this.in_requestDecomposer = requestDecomposer.In_Decompose;

            var lineMultiplier = new MessageMultiplier<string>(2);

            var configMultiplier = new MessageMultiplier<string>(2);
            this.in_configMultiplier = configMultiplier.In_Multiply;

            requestDecomposer.Out0 += writer.In_WriteFile;
            requestDecomposer.Out1 += reader.In_ReadFile;

            configMultiplier.OutputPins[0] = writer.In_Configure;
            configMultiplier.OutputPins[1] = reader.In_Configure;

            reader.Out_Line += trans.In_TransformLine;
            trans.Out_TransformedLine += lineMultiplier.In_Multiply;

            lineMultiplier.OutputPins[0] = writer.In_WriteLine;
            lineMultiplier.OutputPins[1] = line => this.Out_TransformedLine(line);
        }


        public void In_TransformToFile(FileTransformationRequest request)
        {
            this.in_requestDecomposer(request);
        }


        public void In_ConfigureDefaultPath(string defaultPath)
        {
            this.in_configMultiplier(defaultPath);
        }


        public event Action<string> Out_TransformedLine;
    }
}
